package com.qianf.qfoffice.controller;
import com.github.pagehelper.PageInfo;
import com.qianf.qfoffice.entity.Exercises;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.service.ExercisesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;


/**
 * 练习相关控制器
 */
@Controller
@RequestMapping("/exer")

public class ExercisesController {

    @Autowired
    private ExercisesService exercisesService;

    /**
     * 添加习题
     * @param exercises
     * @return
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public ResponseData add(Exercises exercises){

        ResponseData<Object> responseData = new ResponseData<>();

        boolean result = exercisesService.addExercises(exercises);

        if(result==true){
            responseData.setCode(1);
            responseData.setData(exercises);
        }else{
            responseData.setCode(0);
            responseData.setMsg("添加失败");
        }

        return responseData;
    }

    /**
     * 根据用户id展示所有习题列表
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/select.do")
    @ResponseBody
    public ResponseData select(Integer page, Integer limit){


        List<Exercises> exercisesList = exercisesService.selectExercises(page,limit);

        //得到分页相关信息
        PageInfo<Exercises> exercisesPageInfo = new PageInfo<>(exercisesList);

        ResponseData<Object> responseData = new ResponseData<>();

        responseData.setCode(0);
        responseData.setCount((int) exercisesPageInfo.getTotal());
        responseData.setData(exercisesList);
        return responseData;
    }

    /**
     * 删除习题
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public ResponseData delete(int id){
        ResponseData<Object> responseData = new ResponseData<>();

        boolean result = exercisesService.deleteExerciseById(id);

        if(result==true){
            responseData.setCode(1);
            responseData.setMsg("删除成功");

        }else{
            responseData.setData(0);
            responseData.setMsg("删除失败");
        }

        return responseData;

    }

}
